package com.example.module_park.ui

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.lib_common.base.BaseActivity
import com.example.lib_common.base.state.Empty
import com.example.lib_common.base.state.Failed
import com.example.lib_common.base.state.Loading
import com.example.lib_common.base.state.Success
import com.example.lib_common.observeWithLifecycle
import com.example.lib_common.utils.RouterPath
import com.example.module_park.R
import com.example.module_park.adapter.MultiEnterpriseAdapter
import com.example.module_park.databinding.ActivityEnterpriseBinding
import com.example.module_park.enterprise.EnterpriseViewModel
import com.example.module_park.model.EnterpriseEntity
import com.example.module_park.model.MultiEnterpriseItem
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.ENTERPRISE_UI)
class EnterpriseActivity : BaseActivity<EnterpriseViewModel, ActivityEnterpriseBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_enterprise
    private val mViewModel: EnterpriseViewModel by viewModels()

    private lateinit var mAdapter: MultiEnterpriseAdapter // 声明适配器变量
    private var originalEnterpriseList = listOf<EnterpriseEntity>() // 存储原始企业列表

    override fun initView() {
        super.initView()

        // 初始化适配器
        mAdapter = MultiEnterpriseAdapter(emptyList()) // 初始化为空列表
        // 设置布局管理器
        mBinding.rvEnterprise.layoutManager=LinearLayoutManager(this)
        mBinding.rvEnterprise.adapter = mAdapter

        // 设置搜索回调
        mAdapter.setOnSearchQueryChangeListener { query ->
            // 处理搜索查询
            val filteredList = if (query.isEmpty()) {
                originalEnterpriseList // 查询为空时显示全部
            } else {
                originalEnterpriseList.filter {
                    it.name?.contains(query, ignoreCase = true) ?: false
                }
            }

            val updatedItems = listOf(
                MultiEnterpriseItem.Search("搜索企业..."), // 保留搜索项
                MultiEnterpriseItem.VerticalList(filteredList) // 过滤后的企业列表
            )

            // 更新适配器数据
            mAdapter.updateItems(updatedItems)
        }
        //返回首页
        val headerLayout = findViewById<View>(R.id.header_title)
        val back = headerLayout.findViewById<ImageView>(R.id.enterprise_header_back)
        val title = headerLayout.findViewById<TextView>(R.id.enterprise_header_title)
        back.setOnClickListener {
            finish()
        }
        title.setOnClickListener {
            ARouter.getInstance().build(RouterPath.APPLICATION_UI).navigation()
        }


    }

    override fun initData() {
        mViewModel.getEnterpriseList()
    }
    override fun initListener() {
        mViewModel.enterpriseState.observeWithLifecycle(this){
            when(it){
                Empty->{

                }
                is Failed->{

                }
                Success->{
                    // 保存原始企业列表副本
                    originalEnterpriseList = mViewModel.enterpriseList.toList()

                    // 构建适配器数据
                    val updatedItems = listOf(
                        MultiEnterpriseItem.Search("搜索企业..."),
                        MultiEnterpriseItem.VerticalList(originalEnterpriseList)
                    )

                    // 更新适配器
                    mAdapter.updateItems(updatedItems)
                }
                Loading->{

                }
            }
        }

    }

}